package com.compiler.model;

import java.util.ArrayList;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.VariableElement;
import javax.lang.model.util.Elements;


/**
 * Created by huilin on 2017/12/9.
 */

public class AnnotatedMvpClass {
    private ArrayList<MvpModelType> mvpModelTypes;
    private Elements elements;

    public AnnotatedMvpClass(Elements elements) {
        this.elements = elements;
        mvpModelTypes = new ArrayList<>();
    }
    public void addMvpModelType(MvpModelType mvpModelType){
        mvpModelTypes.add(mvpModelType);
    }
    public void write(){

    }

    public JavaFile generateFile(){
        //生成class
        TypeSpec.Builder classBuilder = TypeSpec.classBuilder(mvpModelTypes.get(0).mClassName+"_Mvp")
                .addModifiers(Modifier.PUBLIC);

        for (MvpModelType mvpModelType : mvpModelTypes){
            classBuilder.addSuperinterface(ClassName.get(mvpModelType.getModelName()));
            int i = 0;
            for (ExecutableElement executableElement : mvpModelType.mMethods){
                MethodSpec.Builder builder = MethodSpec.overriding(executableElement);
                builder.addStatement("$T service = $T.getInstance().create($T.class)",mvpModelType.mvpServiceType.getModelName(),ClassName.get("xtu.www.viewinject.net","NetTask"),mvpModelType.mvpServiceType.getModelName());
                if(i<mvpModelType.mvpServiceType.mMethods.size()){
                    ExecutableElement executableElement1 = mvpModelType.mvpServiceType.mMethods.get(i);
                    StringBuilder stringBuilder = new StringBuilder("service.");
                    stringBuilder.append(executableElement1.getSimpleName().toString());
                    stringBuilder.append("(");
                    String callBack = "";
                    for (VariableElement value : executableElement.getParameters()){
                        if( ClassName.get("xtu.www.viewinject.net","MMCallback").toString().equals(ClassName.get(value.asType()).toString())){
                            callBack = value.getSimpleName().toString();
                        }else{
                            stringBuilder.append(value.getSimpleName().toString());
                            if(i<executableElement.getParameters().size()-2){
                                stringBuilder.append(",");
                            }
                        }

                    }
                    stringBuilder.append(")");
                    stringBuilder.append(".subscribeOn($T.io())");
                    stringBuilder.append(".observeOn($T.mainThread())");
                    if(callBack.length()>0){
                        stringBuilder.append(".subscribe(NetTask.getInstance().onSubscriber("+callBack+"))");
                    }
                    builder.addStatement(stringBuilder.toString(),ClassName.get("rx.schedulers","Schedulers"),ClassName.get("rx.android.schedulers","AndroidSchedulers"));
                }
                classBuilder.addMethod(builder.build());
                i++;
            }
        }
        return JavaFile.builder(mvpModelTypes.get(0).mPackName,classBuilder.build()).build();
    }
}
